//
//  ZYViewController.m
//  MusicMV
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "ZYViewController.h"

@interface ZYViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *hearView;
@property (nonatomic, retain) NSMutableArray *mArr;// 装满model
@property (nonatomic, retain) NSMutableArray *tempArr;
@property (nonatomic, retain) UIImageView *imgeView;
@property (nonatomic, assign) NSInteger number;
@end

@implementation ZYViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self shujujiexi];
    [self create];
    
    
}

- (void)create {
    self.hearView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
    _hearView.backgroundColor = [UIColor redColor];
    
    self.imgeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
    _imgeView.backgroundColor = [UIColor yellowColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:(UITableViewStylePlain)];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _hearView;
    [_hearView addSubview:_imgeView];
    _tableView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZYTableViewCell class] forCellReuseIdentifier:@"ZYTableViewCell"];
//    http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/track?device=iPad&key=ranking%3Atrack%3Aplayed%3A1%3A0&pageId=1&pageSize=20
//
//    http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios-b1&device=iPad&includeActivity=true&includeSpecial=true&scale=3&version=4.3.38
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self shujujiexi];
        
    }];
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        
    }];
    
}
#pragma mark - item个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ZYSModel *model = _mArr[section];
    NSArray *arr = model.list;
    return arr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYTableViewCell"];
    ZYSModel *model = _mArr[indexPath.section];
    NSArray *arr = model.list;
    cell.lmodel = arr[indexPath.row];

    return cell;
    
    
}
#pragma mark - 分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _mArr.count;
    
}
#pragma mark - row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return HEIGHT / 8 + 10;
    
}
#pragma mark - 头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50;
    
}
#pragma mark - 头的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        ZYSModel *model = _mArr[section];
        return model.title;
    } else {
        ZYSModel *model = _mArr[section];
        return model.title;
    }
    
}
#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYSecondViewController *vc = [[ZYSecondViewController alloc] init];
    ZYSModel *model = _mArr[indexPath.section];
    vc.model = model.list[indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)shujujiexi {
    
    [APPTools GETWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios-b1&device=iPad&includeActivity=true&includeSpecial=true&scale=3&version=4.3.38" par:nil success:^(id responseObject) {
        
        NSArray *arr = responseObject[@"datas"];
        self.mArr = [ZYSModel modelWithArray:arr];
        
        
        
        NSDictionary *tempDic = responseObject[@"focusImages"];
        NSArray *tArr = tempDic[@"list"];
        
        self.tempArr = [ZYSMMModel modelWithArray:tArr];
        [_tableView reloadData];
        ZYSMMModel *model = _tempArr.firstObject;
        [_imgeView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
        //停止
        [_tableView.mj_header endRefreshing];
    } filed:^(NSError *error) {
        
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
